package com.example.orchid.paper.util;

import android.text.TextUtils;

import com.example.orchid.paper.entity.BaseEntity;
import com.example.orchid.paper.entity.NewsListEntity;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * GsonUtils
 * Created by Orchid on 2015-10-12.
 */
public class GsonUtils {
    public static ArrayList<NewsListEntity.NewsEntity> getNewsList(String content) {
        if (TextUtils.isEmpty(content)) {
            return null;
        }

        Gson gson = new Gson();
        try {
            NewsListEntity newsListEntity = gson.fromJson(content,
                    NewsListEntity.class);
            return newsListEntity != null ? newsListEntity.stories : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解析一个字符串，得到BaseEntity对象
     *
     * @param content
     * @param clazz
     * @return
     */
    public static BaseEntity getEntity(String content, Class<?> clazz) {
        if (TextUtils.isEmpty(content)) {
            return null;
        }
        Gson gson = new Gson();

        try {
            BaseEntity baseEntity = (BaseEntity) gson.fromJson(content, clazz);
            return baseEntity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
